<?php defined('SYSPATH') OR die('No direct access allowed.');

abstract class Backend_Controller extends Frontend_Controller {

	public $role_required = 'admin'; // role_id OR role_name if required

	public function __construct() {
		parent::__construct();
		if (Router::$method!=='confirm' AND $this->is_confirmed()===FALSE) {
			$this->_keep_referrer(TRUE);
			// необходимо подтвердить пароль для доступа в админку
			url::redirect(url::base().Router::uri(Router::$current_route, array('controller'=>Router::$controller, 'method'=>'confirm')));
		}
		if ($this->is_confirmed() === TRUE) {
			$this->keep_confirmed();
		}
	}

	public function confirm() {
		$this->history = FALSE;
		// для входа в админку необходимо подтвердить введенный ранее пароль
		if ($data = $this->input->post()) {
      if ($this->auth->login($this->user->username, $data['password'])) {
        $this->set_confirmed(TRUE);
				if ( !( $ref = $this->get_referrer() ) ) {
        // отправляем на индексную страницу соответствующей админки
          $ref = Router::uri(Router::$current_route, array('controller'=>Router::$controller));
				}
        url::redirect($ref);
      }
      else {
				//$this->keep_referrer();
				message::add(Kohana::lang('admin.confirm.incorrect_password'), 'error');
      }
    }
		//else $this->keep_referrer();
		// dont show navigation menu
		$this->template->nav = NULL;
    $content = new View('admin/confirm');
    $this->template->content = $content;
	}

	protected function is_confirmed() {
		return $this->session->get($this->config['confirm_key']);
	}

	protected function set_confirmed($value = TRUE) {
		$this->session->set_flash($this->config['confirm_key'], $value);
	}

	protected function keep_confirmed() {
		$this->session->keep_flash($this->config['confirm_key']);
	}

}